package ByteTree;

import java.io.ByteArrayOutputStream;

import Verifier.GeneralUtil;

/**
 * @author Hadar Miller
 * 
 */

public class Leaf extends Node{

	byte[] data;
	int numOfBytes;
	/**
	 * 
	 * @param num the number of data bytes.
	 * @param byteArray data bytes.
	 */
	public Leaf(int num, byte[] byteArray) throws Exception {
		super(0,null);
		this.numOfBytes = num;
		this.data = byteArray;
	}

	/**
	 * @return data bytes of the current leaf.
	 */
	public byte [] getData() {
		return data;
	}
	
	/**
	 * 
	 * @return number of data bytes.
	 */
	public int getNumOfBytes() {
		return numOfBytes;
	}
	

	public boolean isLeaf() {
		return true;
	}
	
	/**
	 * converts the leaf into its byte array representation.
	 * @return byte array representation of the leaf.
	 */
	public byte[] toByteArray() throws Exception 
	{
		ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
		byteArrayStream.write((byte)0x01);		
		byteArrayStream.write(GeneralUtil.intToByteArray(numOfBytes));
		byteArrayStream.write(data);
		return byteArrayStream.toByteArray();
	}
}
